package dhm.com.hpx.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.utils.CleanDataUtils;
import dhm.com.hpx.utils.SpUtils;

import static dhm.com.hpx.App.getContext;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.cache)
    TextView cache;
    @BindView(R.id.def_no)
    ImageView defNo;
    @BindView(R.id.def_yes)
    ImageView defYes;
    @BindView(R.id.quan)
    ImageView quan;

    private boolean isDef = false;

    @Override
    protected int getLayout() {
        return R.layout.activity_setting;
    }

    @SuppressLint("NewApi")
    @Override
    protected void initView() {
        ButterKnife.bind(this);
        try {
            String totalCacheSize = CleanDataUtils.getTotalCacheSize(Objects.requireNonNull(getContext()));
            cache.setText("缓存："+totalCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initData() {
        showPopwindow();
    }

    @SuppressLint("NewApi")
    @OnClick({R.id.back, R.id.edit, R.id.yinsi, R.id.repass, R.id.clear, R.id.about, R.id.quan, R.id.signOut})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.edit:
                intent=new Intent(this,MyinfoActivity.class);
                startActivity(intent);
                break;
            case R.id.yinsi:
                intent=new Intent(this,AccountActivity.class);
                startActivity(intent);
                break;
            case R.id.repass:
                intent=new Intent(this,ChangepwdActivity.class);
                startActivity(intent);
                break;
            case R.id.clear:
//                清楚缓存
                CleanDataUtils.clearAllCache(Objects.requireNonNull(getContext()));
                String clearSize = CleanDataUtils.getTotalCacheSize(Objects.requireNonNull(getContext()));
                cache.setText("缓存："+clearSize);
                Toast.makeText(this, "缓存清除成功", Toast.LENGTH_SHORT).show();

                break;
            case R.id.about:
                intent=new Intent(this,AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.quan:
                if (isDef) {
                    quan.setBackgroundResource(R.drawable.back_quan);
                    defNo.setVisibility(View.VISIBLE);
                    defYes.setVisibility(View.GONE);
                } else {
                    quan.setBackgroundResource(R.drawable.back_quans);
                    defYes.setVisibility(View.VISIBLE);
                    defNo.setVisibility(View.GONE);
                }
                isDef = !isDef;
                break;
            case R.id.signOut:
                setBackgroundAlpha(0.5f);
                popWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
                break;
            default:
        }
    }


    private PopupWindow popWindow;

    private void showPopwindow() {
        View popView = View.inflate(this, R.layout.dialog_exit, null);
        TextView cancle = popView.findViewById(R.id.cancle);
        TextView sure = popView.findViewById(R.id.sure);

        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SpUtils.clear(SettingActivity.this);
                Intent intent = new Intent(SettingActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                popWindow.dismiss();
                SpUtils.saveString(SettingActivity.this,"firstcom","1");
                finish();
            }
        });

        popWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setFocusable(true);
        popWindow.setTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.setOutsideTouchable(true);// 设置允许在外点击消失


        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupWindow隐藏时恢复屏幕正常透明度
                setBackgroundAlpha(1.0f);
            }
        });

    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

}
